<template>
  <lay-container :fluid="true" style="padding: 10px;padding-top: 10px;">
    <lay-card>
      <lay-row>
        <lay-col :md="24">
          <lay-form :model="queryModel" ref="layFormRef">
            <lay-row>
              <lay-col :md="10">
                <lay-form-item label="时间范围" prop="rangeTime">
                  <lay-date-picker  v-model="rangeTime" range :placeholder="['开始日期', '结束日期']"></lay-date-picker>
                </lay-form-item>
              </lay-col>
              <lay-col :md="8">
                <lay-form-item label-width="0">
                  <lay-button-group>
                    <lay-button type="primary" @click="GetList(queryModel)" prefix-icon="layui-icon-search">查询
                    </lay-button>
                    <lay-button type="normal" @click="add()" v-permission="PerKey.Dict_AddOrEditView"
                      prefix-icon="layui-icon-addition">新增
                    </lay-button>
                  </lay-button-group>
                </lay-form-item>
              </lay-col>
            </lay-row>
          </lay-form>
        </lay-col>
        <lay-col :md="24">
          <lay-table :page="page" :columns="columns" :dataSource="dataSource" v-model:selectedKeys="selectedKeys"
            @change="change" :size="tablesize">
            <template v-slot:changeval="{ data }">
              <lay-space size="xs">
                <lay-badge theme="green" v-if="data.status">启用</lay-badge>
                <lay-badge theme="gray" v-else>禁用</lay-badge>
              </lay-space>
            </template>
            <template v-slot:operator="{ data }">
              <lay-button-group>
                <lay-button type="primary" size="sm" @click="edit(data.id)" v-permission="PerKey.Dict_AddOrEditView"
                  prefix-icon="layui-icon-edit">编辑</lay-button>
                <lay-button type="danger" size="sm" @click="del(data)" v-permission="PerKey.Dict_Delete"
                  prefix-icon="layui-icon-delete">删除</lay-button>
              </lay-button-group>
            </template>
          </lay-table>
        </lay-col>
      </lay-row>
    </lay-card>

    <lay-layer :title="addoredittip" v-model="modelVisible" :resize="true" :area="['800px']">
      <lay-card style="padding:10px;">
        <lay-row space="10">
          <lay-form :model="form">
            <lay-col :md="24">
              <lay-form-item label="类别" prop="cid" required initValidate required-error-message="类别必选">
                <lay-col md="8">
                  <lay-select v-model="form.cid" :disabled="true">
                    <lay-select-option v-for="cate in cids" :value="cate.id" :label="cate.name"></lay-select-option>
                  </lay-select>
                </lay-col>
              </lay-form-item>
              <lay-col :md="8">
              <lay-form-item label="一级值班人" prop="oneLevel" required>
                <lay-col md="24">
                  <lay-input v-model="form.oneLevel"></lay-input>
                </lay-col>
              </lay-form-item>
            </lay-col>
            <lay-col :md="8">
              <lay-form-item label="二级值班人" prop="twoLevel" >
                <lay-col md="24">
                  <lay-input v-model="form.twoLevel"></lay-input>
                </lay-col>
              </lay-form-item>
            </lay-col>
            <lay-col :md="8">
              <lay-form-item label="三级值班人" prop="threeLevel" >
                <lay-col md="24">
                  <lay-input v-model="form.threeLevel"></lay-input>
                </lay-col>
              </lay-form-item>
            </lay-col>
            <lay-col :md="8">
              <lay-form-item label="其他值班人" prop="otherBody" >
                <lay-col md="24">
                  <lay-input v-model="form.otherBody"></lay-input>
                </lay-col>
              </lay-form-item>
            </lay-col>
            <lay-col :md="8">
              <lay-form-item label="值班电话" prop="phone" required>
                <lay-col md="24">
                  <lay-input v-model="form.phone"></lay-input>
                </lay-col>
              </lay-form-item>
            </lay-col>
              <lay-col :md="24">
                <lay-col :md="12">
                  <lay-form-item label="开始时间" prop="catName">
                    <lay-col md="8">
                      <lay-date-picker v-model="form.startTime" simple type="date"></lay-date-picker>
                    </lay-col>
                  </lay-form-item>
                </lay-col>
                <lay-col :md="12">
                  <lay-form-item label="结束时间" prop="catName">
                    <lay-col md="8">
                      <lay-date-picker v-model="form.endTime" simple type="date"></lay-date-picker>
                    </lay-col>
                  </lay-form-item>
                </lay-col>
              </lay-col>
              <lay-form-item label="备注" prop="remark">
                <lay-col md="24">
                  <lay-input v-model="form.remark"></lay-input>
                </lay-col>
              </lay-form-item>
              <lay-form-item label="状态" prop="status">
                <lay-col md="6" class="tip">
                  <lay-switch v-model="form.status" onswitch-text="启用" unswitch-text="禁用"></lay-switch>
                </lay-col>
                <lay-col md="6"> </lay-col>
              </lay-form-item>

              <lay-col :md="24" class="saveBtn">
                <lay-button @click="submitClick" type="primary" v-permission="PerKey.Dict_AddOrEdit"
                  prefix-icon="layui-icon-ok">
                  {{ addoredittip }}
                </lay-button>
              </lay-col>
            </lay-col>
          </lay-form>
        </lay-row>
      </lay-card>
    </lay-layer>
  </lay-container>
</template>
  
<script lang="ts" name="OndutyData" setup>
import { reactive, ref, toRefs, watch } from "vue";
import { layer } from "@layui/layer-vue";
import { getList, delOne, getEdit, postAdd, putEdit } from "../../../../api/module/oas/onduty";
import { PerKey } from "../../../../library/permissionConst"
import { OndutyData } from "../../../../types/onduty";

interface OndutyDataProps {
  catId: number,
}

const props = withDefaults(defineProps<OndutyDataProps>(), {
  catId: 0,
});

const selectedKeys = ref(["1"]);
const page = ref({ total: 0, limit: 15, current: 1, showCount: true, showRefresh: true, limits: [10, 15, 20, 50, 100, 200] });
const tablesize = ref('md');

const columns = [
  { title: "Id", width: "50px", key: "id", align: "center", },
  { title: "一级值班员", key: "oneLevel", align: "center", },
  { title: "二级值班员", key: "twoLevel", align: "center", },
  { title: "三级值班员", key: "theeLevel", align: "center", },
  { title: "其他值班员", key: "otherBody", align: "center", },
  { title: "值班电话", width: "160px", key: "phone", align: "center", },
  { title: "开始时间", width: "160px", key: "startTime", align: "center", },
  { title: "结束时间", width: "160px", key: "endTime", align: "center", },
  { title: "状态", customSlot: "changeval", width: "80px", align: "center", },
  { title: "操作", width: "160px", customSlot: "operator", key: "operator", align: "center", }
]
const dataSource = ref([])
//定义模型
const putData = reactive({
  queryModel: {
    Cid: props.catId,
    page: page.value.current,
    limit: page.value.limit,
    startTime: "",
    endTime: "",
  }
})
const { queryModel } = toRefs(putData)
const rangeTime = ref([])


//获取列表
const GetList = function (query: any) {
  if (query != null) {
    queryModel.value.startTime = rangeTime.value[0]
    queryModel.value.endTime = rangeTime.value[1]
    queryModel.value.Cid = props.catId
  }
  getList(queryModel.value).then((res) => {
    if (res.status == 1) {
      dataSource.value = res.data.data
      page.value.total = res.data.count
    }
  })
}

//监听参数变化
watch(() => props.catId, (Cid) => {
  queryModel.value.Cid = Cid as unknown as number
  GetList(queryModel.value)
}, { immediate: true, deep: true })


const layFormRef = ref();
const reset = function () {
  layFormRef.value.reset();
}

const change = function ({ current, limit }: any) {
  queryModel.value.Cid = props.catId
  queryModel.value.page = current
  queryModel.value.limit = limit
  GetList(queryModel.value);
};

const edit = function (id: number) {
  changeVisible(id)
}
const add = function () {
  changeVisible(0)
}

//添加或者编辑
const cids = ref({ targetList: [] as any, })

const modelVisible = ref(false)
const data = reactive({
  form: <OndutyData><unknown>[]
})

const { form } = toRefs(data)

let addoredittip = ref();

const changeVisible = function (id: number) {
  modelVisible.value = !modelVisible.value
  if (id == 0) {
    addoredittip.value = "新增";
    getEdit(id).then((res) => {
      cids.value = res.data.catList
      form.value = {
        id: 0,
        cid: props.catId,
        oneLevel: "",
        twoLevel: "",
        threeLevel: "",
        otherBody: "",
        phone: "",
        startTime: new Date,
        endTime: new Date,
        remark: "",
        sort: 1,
        status: true
      }
    })
    return
  } else {
    addoredittip.value = "编辑";
    getEdit(id).then((res) => {
      cids.value = res.data.catList
      if (res.status == 1) {
        let data = res.data.data
        form.value = data
      }
    })
    return
  }
}

const submitClick = function () {
  if (form.value.cid < 1) {
    layer.msg("类别不能空")
    return
  }

  if (form.value.id == 0) {
    postAdd(form.value).then((res) => {
      done(res)
    });
  } else {
    putEdit(form.value).then((res) => {
      done(res)
    });
  }
};
const done = (res: any) => {
  if (res.status == 1) {
    layer.msg(res.msg, { time: 1000, icon: 1 }, () => { });
    modelVisible.value = !modelVisible.value
    GetList(queryModel.value)
  } else {
    layer.msg(res.msg, { icon: 2 });
    return false;
  }
}

//删除
const del = function (data: any) {
  layer.confirm("确认删除ID: " + data.id + " 值班吗？", {
    title: "警告", move: "true", shade: "false", shadeClose: "true",
    btn: [{
      text: '确认', callback: (layerId: number) => {
        delOne(data.id).then((res) => {
          if (res.status == 1) {
            layer.msg(res.msg, { time: 1000, icon: 1 }, () => {
              GetList(queryModel.value)
            });
          } else {
            layer.msg(res.msg);
          }
          layer.close(layerId);
        })
      }
    }, {
      text: '取消', callback: function (layerId: number) {
        layer.close(layerId);
      }
    }]
  })
}

</script>
<style scoped>

</style>